package com.dd.cloud.user.util;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 极光推送工具类
 * @Author: czg
 * @Date: 2024/1/25 10:06
 * @Version: 1.0.0
 */
@Slf4j
public class JiGuangPushUtil {

    /**
     * 两个参数分别填写你申请的masterSecret和appKey##
     */
    private static String masterSecret = "afc72e8d91772eec8862eb08";
    private static String appKey = "4acf2bde3b2110e3202161fc";
    private static JPushClient jPushClient = new JPushClient(masterSecret, appKey);

    /**
     * 全平台推送
     *
     * @param parm map里面可以根据自身情况增加一些参数 比如页面跳转的地址等
     * @param list 用户id集合 如果为空 则为全部推送
     */
    public static boolean jpushAll(Map<String, String> parm, List<String> list) {
        PushPayload payload = PushPayload.newBuilder()
                //所有平台的用户
                .setPlatform(Platform.all())
                //指定用户 用id指定
                .setAudience((list == null || list.isEmpty()) ? Audience.all() : Audience.registrationId(list))
                .setNotification(Notification.newBuilder()
                        //ios推送
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(IosAlert.newBuilder().setTitleAndBody(parm.get("title"), null, parm.get("body")).build())
                                .setBadge(+1)
                                .setSound("qxgf")
                                .addExtras(parm)
                                .build())

                        //Android推送 也可以分开写
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(parm.get("title"))
                                .addExtras(parm)
                                .setAlert(parm.get("body"))
                                .build())
                        .build())
                //指定开发环境
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                //自定义信息
                .setMessage(Message.newBuilder().setMsgContent(parm.get("title")).addExtras(parm).build())
                .build();
        try {
            PushResult pushResult = jPushClient.sendPush(payload);
            log.info("极光推送结果:" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                return true;
            }
            return false;
        } catch (APIConnectionException e) {
            log.info("极光推送失败:");
            e.printStackTrace();
        } catch (APIRequestException e) {
            log.info("极光推送失败:");
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 参数设置
     *
     * @param title
     * @param body
     * @param list  用户集合
     */
    public static void jpushPush(String title, String body, List<String> list) {
        Map<String, String> parm = new HashMap<>();
        //标题
        parm.put("title", title);
        //内容
        parm.put("body", body);
        //用户
        jpushAll(parm, list);
    }
}
